<template>
     <view class="login">
		 <view class="lang" @click="changelang()">{{ i18n.lang }}</view>
		 <view class="form w90">
			 <view class="with-fun">
			 	<view class="fasticon icon-yonghu-tianchong"></view>
			 <input class="uni-input" :placeholder="i18n.phone" v-model="mobile" @input="bindClearInput" />
			 <view class="icon icon-close1" v-if="showClearIcon" @click="clearIcon"></view>
			 </view>
			 <view class="with-fun">
				 <view class="fasticon icon-mima1"></view>
			 	<input class="uni-input" v-model="password" :placeholder="i18n.password" :password="showPassword" />
			 	<view class="icon" :class="[showPassword ? 'icon-yincang' : 'icon-chakan']" @click="changePassword"></view>
			 </view>
		 </view>
		 <view class="login-but w90">
			 <button type="button" @click="login"><span>{{i18n.SignIn}}</span></button>
		 </view>
		 <view class="login-item w90">
		 	<view class="uni-grid-9 uni-common-mt">
		 		<view class="tal" @click="goreg">
		 			{{i18n.register}}
		 		</view>
				<view class="tac astyle">
					<a href="https://fir.im/aqhu">{{i18n.download}}</a>
				</view>
				<view class="tar" @click="goForget">
					{{i18n.forget}}
				</view>
		 	</view>
		 </view>
	 </view>
</template>

<script>
import service from '../../service.js';
import global_ from '../../global.js';
import encryptRSA from '../../static/utils/security.js';
import { mapState, mapMutations } from 'vuex';

export default {
    data() {
        return {
            providerList: [],
            hasProvider: false,
            account: '',
            password: '',
            positionTop: 0,
            showPassword: true,
            showClearIcon: false,
            mobile: ''
        };
    },
    computed: {
        i18n() {
            return this.$t('index');
        }
    },
    methods: {
        changelang(e) {
            console.log(this.$i18n.locale);
            if (this.$i18n.locale == 'en') {
                this.$i18n.locale = 'zh';
            } else {
                this.$i18n.locale = 'en';
            }
        },
        changePassword: function() {
            this.showPassword = !this.showPassword;
        },
        bindClearInput: function(e) {
            this.inputClearValue = e.target.value;
            if (e.target.value.length > 0) {
                this.showClearIcon = true;
            } else {
                this.showClearIcon = false;
            }
        },
        goreg: function() {
            uni.navigateTo({
                url: '/pages/reg/reg'
            });
        },
        goForget: function() {
            uni.navigateTo({
                url: '/pages/forget/index'
            });
        },
        clearIcon: function() {
            this.mobile = '';
            this.showClearIcon = false;
        },
        getuser: function() {
            let userinfo = uni.getStorageSync('userinfo');
            if (userinfo !== null) {
                if (userinfo.token === '') {
                } else {
                    uni.redirectTo({
                        url: '/pages/home/index'
                    });
                    if (lang === null) {
                        this.langindex = this.langcn;
                    } else if (!lang) {
                        this.langindex = this.langen;
                    } else {
                        this.langindex = this.langcn;
                    }
                }
            } else if (userinfo === null) {
            }
        },
        login: function() {
            // 登录
            let that = this;
            let mobile = this.mobile;
            let password = this.password.toString().length;
            if (mobile === '') {
                uni.showToast({ title: '请输入手机号', icon: 'none' });
            } else if (password < 6) {
                uni.showToast({ title: '请输入密码', icon: 'none' });
            } else {
                // this.flag = true
                uni.request({
                    url: global_.url.login,
                    data: {
                        account: that.mobile,
                        password: encryptRSA(this.password)
                    },
                    success: function(result) {
                        if (result.data.code === 0) {
                            uni.showToast({ title: result.data.msg, icon: 'none' });
                        } else {
                            uni.showToast({ title: result.data.msg, icon: 'success' });
                            that.token = result.data.data.userinfo.token;
                            that.userinfo = result.data.data.userinfo;
                            uni.setStorageSync('userinfo', that.userinfo);
                            // 获取登录信息
                            that.userinfo = {
                                isLogin: true,
                                data: that.userinfo
                            };
                            uni.navigateTo({
                                url: '/pages/home/index'
                            });
                        }
                    },
                    fail: function({ errMsg }) {
                        uni.showToast({ title: '请求失败' });
                    }
                });
            }
        }
    },
    onLoad() {
		this.getuser()
	}
};
</script>
